package com.example.test.conview.view.core;

import static com.puty.conputyview.common.PutyConst.HOUSEHOLD_SERIES;
import static com.puty.conputyview.common.PutyConst.IMAGE;
import static com.puty.conputyview.common.PutyConst.INDUSTRY_SERIES;
import static com.puty.conputyview.common.PutyConst.SERIES_TYPE;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;

import com.puty.conputyutils.BitmapUtils;
import com.puty.conputyutils.LogUtils;
import com.puty.conputyview.common.PutyConst;
import com.puty.conputyview.view.tool.Smooth;

/**
 * 图片属性处理
 */
public class ImageElement extends Element{
    private final static  String TAG = "ImageElement";

    public Bitmap img;
    /**
     * 是否缩放
     */
    public int isscale = 1;

    public ImageElement(Context context, Bitmap img, float width, float height, String imageUrlString,
                        Label lb) {
        super(context, lb);
        super.type = IMAGE;
        this.width = width;
        this.height = height;
        this.imageUrlString = imageUrlString;
        if (img == null) {
            downloadImage(context, imageUrlString);
        } else {
            this.img = img;
            this.tempBitmap = img;
            init();
        }
    }

    public ImageElement(Context context, Bitmap img, float width, float height,Label lb) {
        super(context,lb);
        super.type = IMAGE;
        this.img = img;
        this.tempBitmap = img;
        this.width = width;
        this.height = height;
        init();
    }

    @Override
    public void init() {
        super.init();
        rate(this.rate);
    }

    @Override
    public void rate(int degree) {
        super.rate(degree);
        this.img = tempBitmap;
        if (this.img == null) {
            LogUtils.e(TAG, "rate ---> img == null");
            return;
        }
        Matrix matrix = new Matrix();
        matrix.setRotate(degree, this.img.getWidth() / 2, this.img.getHeight() / 2);
        this.img = Bitmap.createBitmap(this.img, 0, 0, this.img.getWidth(), this.img.getHeight(), matrix, true);
        rotation(degree);
    }

    @Override
    public void zoom(float x, float y, float width, float height) {
        //不允许缩放
        if (this.isscale == 0) {
            LogUtils.e(TAG, "zoom ---> this.isscale == 0, 不允许缩放");
            return;
        }
        super.zoom(x, y, width, height);
        // 判断是是否超出width的边界
        if (this.left + this.width >= width) {
            LogUtils.e(TAG, "zoom ---> this.left + this.width >= width");
            return;
        }
        // 判断是是否超出height的边界
        if (this.top + this.height >= height) {
            LogUtils.e(TAG, "zoom ---> this.top + this.height >= height");
            return;
        }
        if (this.zoomdirect == 0) {// 往X方向滑动
            this.width += x;
            if (this.width <= PutyConst.RATE_BOUNDARY_100) {
                this.width = PutyConst.RATE_BOUNDARY_100;
            } else if (this.left + this.width >= width) {
                this.width -= x;
                return;
            }
        } else if (this.zoomdirect == 1) {//往Y方向
            this.height += y;
            if (this.height <= PutyConst.RATE_BOUNDARY) {
                this.height = PutyConst.RATE_BOUNDARY;
            } else if (this.top + this.height >= height) {
                this.height -= y;
                return;
            }
        } else {
            float z = (x / this.width) * this.height;
            this.height += z;
            this.width += x;
            if (this.width <= PutyConst.RATE_BOUNDARY) {
                this.height = (this.height / this.width) * PutyConst.RATE_BOUNDARY;
                this.width = PutyConst.RATE_BOUNDARY;
            } else if (this.left + this.width >= width) {
                this.height -= z;
                this.width -= x;
                return;
            } else if (this.height <= PutyConst.RATE_BOUNDARY) {
                this.width = (this.width / this.height) * PutyConst.RATE_BOUNDARY;
                this.height = PutyConst.RATE_BOUNDARY;
            } else if (this.top + this.height >= height) {
                this.height -= z;
                this.width -= x;
                return;
            }
        }
        zoomOtherSelected(x, y);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        draw(canvas,false);
    }

    @Override
    public void draw(Canvas canvas, boolean isPrinting) {
        super.draw(canvas, isPrinting);
        // 家用系列
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            // 等比例缩放并居中显示
            float scaleW = this.img.getWidth() / width;
            float scaleH = this.img.getHeight() / height;
            float scale = Math.max(scaleW,scaleH);
            float imageW = this.img.getWidth() / scale;
            float imageH = this.img.getHeight() / scale;
            Bitmap bitmap = Smooth.rescale(this.img, (int)imageW, (int)imageH, Smooth.Algo.values()[0]);
            float bitmapLeft = (width - imageW) / 2;
            float bitmapTop = (height - imageH) / 2;
            if(isblack == 0 && isPrinting){
                canvas.drawBitmap(BitmapUtils.toGrayscale(bitmap),left + bitmapLeft,top+bitmapTop,null);
            }else {
                canvas.drawBitmap(bitmap,left + bitmapLeft,top+bitmapTop,null);
            }
        }
        if (SERIES_TYPE == INDUSTRY_SERIES) {
            Rect sr = new Rect(0, 0, this.img.getWidth(), this.img.getHeight());
            LogUtils.i(TAG,"isblack:"+isblack+":isPrinting:"+isPrinting);
            if(isblack == 0 && isPrinting){
                canvas.drawBitmap(BitmapUtils.toGrayscale(this.img), sr, new RectF(left, top, left + width, top + height), null);
            }else {
                canvas.drawBitmap(this.img, sr, new RectF(left, top, left + width, top + height), null);
            }

            if (isselected && isscale == 1 && isLock == 0) {
                //缩放图标
                RectF rbRect = new RectF(left + width - scaleImageWidth / 2,
                        top + height - scaleImageWidth / 2,
                        left + width + scaleImageWidth / 2,
                        top + height + scaleImageWidth / 2);

                canvas.drawBitmap(puBitmap, new Rect(0, 0, puBitmap.getWidth(),
                        puBitmap.getHeight()), rbRect, null);

                //左上角删除图标
                float hl = this.left - this.deleteBitmap.getWidth() / 2;
                float ht = this.top - this.deleteBitmap.getHeight() / 2;
                float hr = this.left + this.deleteBitmap.getWidth() / 2;
                float hb = this.top + this.deleteBitmap.getHeight() / 2;
                Rect rs = new Rect(0, 0, this.deleteBitmap.getWidth(), this.deleteBitmap.getHeight());
                RectF ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.deleteBitmap, rs, ds, null);

                //画缩放框
                //画右边缩放图标
                hl = this.left + width - this.puhBitmap.getWidth() / 2;
                ht = this.top + height / 2 - this.puhBitmap.getHeight() / 2;
                hr = this.left + width + this.puhBitmap.getWidth() / 2;
                hb = this.top + height / 2 + this.puhBitmap.getHeight() / 2;
                rs = new Rect(0, 0, this.puhBitmap.getWidth(), this.puhBitmap.getHeight());
                ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.puhBitmap, rs, ds, null);

                //画底部缩放图标
                hl = this.left + width / 2 - this.puvBitmap.getWidth() / 2;
                ht = this.top + height - this.puvBitmap.getHeight() / 2;
                hr = this.left + width / 2 + this.puvBitmap.getWidth() / 2;
                hb = this.top + height + this.puvBitmap.getHeight() / 2;
                rs = new Rect(0, 0, this.puvBitmap.getWidth(), this.puvBitmap.getHeight());
                ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.puvBitmap, rs, ds, null);
            }
        }

    }

    /**
     * 复制对象
     *
     * @param element 原始对象
     * @param imageElement 新的对象
     * @return 新的对象
     */
    public ImageElement clone(Element element, ImageElement imageElement) {
        imageElement.scale = element.scale; // 缩放比例
        imageElement.isPrinter = element.isPrinter; // 0 不是打印对象 1打印
        imageElement.tempBitmap = element.tempBitmap; // 原始图
        imageElement.left = element.left; // 元素左侧距离
        imageElement.top = element.top; // 元素顶部距离
        imageElement.rate = element.rate; // 旋转角度
        imageElement.isblack = element.isblack; // 黑白显示
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            imageElement.width = element.width; // 元素宽
            imageElement.height = element.height; // 元素高
        }
        if (SERIES_TYPE == INDUSTRY_SERIES) {
            imageElement.isShow = element.isShow; // 是否显示
            imageElement.type = element.type; // 元素类型
            imageElement.isMirror = element.isMirror; // 镜像
            imageElement.grayYZ = element.grayYZ; //灰度阈值镜像
        }
        return imageElement;
    }

    /**
     * ghfa 图片下载
     *
     * @param context 上下文
     * @param imageUrlString 路径
     */
    private void downloadImage(final Context context, String imageUrlString) {

    }
}
